home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / chkutod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  1.6 KB  |  82 lines

  1. /*
  2.    check if a file is up to date.
  3.    chkutod file [-d n]
  4.                 [-h n]
  5.    result is true or false.
  6.       -d n     allows n days difference
  7.       -h n     allows n hours difference
  8.       -not     inverses result
  9.    
  10. */
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15.  
  16. main(argc,argv)
  17. int argc;
  18. char *argv[];
  19. {
  20. int i,n,m,err,passed,hours,days,maxdiff;
  21. time_t acttime,filetime;
  22. char c,s[80],z[80];
  23. struct stat stbuf;
  24.  
  25.    maxdiff = 0;
  26.    i = 1;
  27.  
  28.    if(checkopt(argc,argv,"-h",z)) maxdiff = 3600 * atoi(z);
  29.    if(checkopt(argc,argv,"-d",z)) maxdiff = 86400 * atoi(z);
  30.    if(checkopt(argc,argv,"-not",z)) i = -1;
  31.    if(maxdiff==0) help();
  32.  
  33.  
  34.    acttime=time(NULL);                        /* get actual time */
  35.    
  36.    err = stat(argv[1],&stbuf);
  37.    filetime = stbuf.st_mtime;                 /* last modification */
  38.  
  39.    passed = difftime(acttime,filetime);       /* number of seconds passed */
  40.  
  41.    hours = passed / 3600;
  42.    days  = hours / 24;
  43.  
  44.    n = maxdiff - passed;
  45.    n = i * n;
  46.  
  47.    if(n > 0) exit(0);
  48.    exit(-1);
  49.  
  50. }
  51.  
  52. help()
  53. {
  54.  
  55. printf("check if a file is up to date.\n");
  56. printf("chkutod file [-d n]\n");
  57. printf("             [-h n]\n");
  58. printf("result is true or false.\n");
  59. printf("   -d n     allows n days difference\n");
  60. printf("   -h n     allows n hours difference\n");
  61. printf("   -not     inverses result\n");
  62. exit(0);
  63. }
  64.  
  65.  
  66. checkopt(argc,argv,s,sv)
  67. int argc;
  68. char *argv[],s[],sv[];
  69. {
  70. int   n,m,i,erg;
  71. char  c,z[80];
  72.  
  73.    erg=0;
  74.    for(n=1;n<argc;n++) {
  75.       if(strcmp(argv[n],s)==0) {
  76.          erg = -1;
  77.          strcpy(sv,argv[n+1]);
  78.       }
  79.    }
  80.    return(erg);
  81. }
  82.